package cn.ubeta.b2c.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import cn.ubeta.b2c.R;
import cn.ubeta.b2c.api.ApiUser;
import cn.ubeta.b2c.bean.ApiResp;
import cn.ubeta.b2c.common.AnalyseActivity;
import cn.ubeta.b2c.utils.AppConfig;
import cn.ubeta.b2c.utils.DialogHelper;
import cn.ubeta.b2c.utils.NetworkCallback;
import cn.ubeta.b2c.utils.ToastHelper;

import java.io.IOException;

/**
 * Created by denny on 16/9/5.
 */

public class UserInfoActivity extends AnalyseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_user_info);

        findViewById(R.id.tv_logout).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.tv_logout == id) {
            DialogHelper.showConform(this, "退出", "确定退出登录吗?", (dialog, which) -> doLogout());
        }
    }

    /**
     * 退出登录
     */
    private void doLogout() {
        ApiUser.logout(new NetworkCallback<ApiResp>() {
            @Override
            public void onResponse(ApiResp resp) {
                if (resp.isSuccess()) {
                    AppConfig.setUid(null);
                    finish();
                } else if (TextUtils.isEmpty(resp.getErrMsg())) {
                    ToastHelper.show("错误:" + resp.getErrCode() + "," + resp.getErrMsg());
                }
            }

            @Override
            public void onFailure(IOException e) {
            }
        });
    }
}
